<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>编辑轮播图</title>
    <link rel="stylesheet" href="../static/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../static/css/public.css" media="all">
    <script src="../static/lib/jquery-3.4.1/jquery-3.4.1.min.js"></script>
    <script src="../static/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
</head>
<style>
    #coverImg{
        height: 250px;
        width: 800px;
        display: none;
    }
    #lookCover{
        display: none;
    }
    #subbtn{
        margin-left: -110px;
    }
</style>
<body>
<div class="layui-form">
    <form id="addForm" class="layui-form layui-form-pane">
        <input type="hidden" id="bannerId" name="bannerId"/>
        <div class="layui-form-item">
            <label class="layui-form-label">宣传课程</label>
            <div class="layui-input-inline">
                <select id="courseId" name="courseId" lay-filter="mySelect">
                    <option value="">请选择课程名称</option>
                    <option th:each="course : ${courses}" th:value="${course.courseId}" th:text="${course.courseName}"></option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">轮播图</label>
            <div class="layui-upload">
                <button type="button" class="layui-btn" id="uploadImg">上传图片</button>
                <button type="button" class="layui-btn layui-btn-normal" id="lookCover">查看图片</button>
                <img class="layui-upload-img"  id="coverImg" alt="课程封面" src="">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button id="subbtn" class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn"></button>
            </div>
        </div>
    </form>
</div>
</body>
<script th:inline="javascript" type="text/javascript">
    let data={
        bannerId: null,
        courseName: null,
        courseId: null,
        bannerUrl: null
    }
    let url;        //添加/修改封面路径
    let imgUrl;     //讲师头像路径
    layui.use(['upload', 'element', 'layer'], function() {
        let $ = layui.jquery
            , upload = layui.upload
            , layer = layui.layer;

        //常规使用 - 普通图片上传
        let uploadInst = upload.render({
            elem: '#uploadImg', url: '/upload/banner', //改成您自己的上传接口
            before: function (e) {
                layer.msg('上传中', {icon: 16, time: 0});
            },
            done: function (res) {
                //如果上传失败
                if (res.code ===200) {
                    url = res.data.url
                    imgUrl=res.data.url
                    $('#coverImg').attr('src', url); //图片链接
                    $('#lookCover').css("display","inline");
                    $('#lookCover').click();
                    return layer.msg('上传成功');
                }else{
                    return layer.msg(res.message);
                }
            },
            error: function () {
                return layer.msg('上传失败');
            }
        });
    });
    $(function (){
        let banner=[[${banner}]];
        let courses=[[${courses}]];
        let submitUrl;
        if(banner===null){
            submitUrl='/banner/addBanner';
            $('#subbtn').html("确认添加");
        }else{
            $('#bannerId').val(banner.bannerId);
            $('#courseName').val(banner.courseName);
            $('#courseId').val(banner.courseId);
            imgUrl=banner.bannerUrl;
            url=banner.bannerUrl;
            $('#lookCover').css("display","inline");
            submitUrl='/banner/editBanner';
            $('#subbtn').html("确认修改");
        }
        layui.form.render(); //重新渲染显示效果

        //提交表单
        $('#addForm').submit(function (e) {
            let formData=new FormData(this);
            data.bannerId=formData.get('bannerId');
            data.courseId=formData.get('courseId');
            data.courseName=$('#courseId option:selected').text();
            if(url!=null){
                data.bannerUrl=url;
            }else{
                layer.msg("封面不能为空");
                e.preventDefault();
            }
            $.ajax({
                type:"post",
                url:submitUrl,
                data:data,
                success:function (res){
                    if(res.code===200){
                        layer.msg(res.message,{time:5000,icon:1,offset:[15]});
                        let index=parent.layer.getFrameIndex(window.name);
                        setTimeout(function (){
                            window.parent.location.reload();//刷新父页面
                            parent.layer.close(index);
                        },1500);
                    }else{
                        layer.msg(res.message,{time:5000,icon:1,offset:[15]});
                    }
                },
                error:function (error){
                    layer.msg(error,{time:5000,icon:2,offset:[15]});
                }
            })
            e.preventDefault();
        });

        //弹出课程封面
        $("#lookCover").click(function () {
            $('#coverImg').attr('src',imgUrl);
            layer.open({
                type: 1,
                title: false,
                closeBtn: 1,
                area: ['auto'],
                skin: 'layui-layer-nobg', //没有背景色
                shadeClose: true,
                content: $('#coverImg'),
                end:function(){
                    $('#coverImg').css("display","none");
                }
            });
        });


    })
</script>
</html>